﻿using GxIAPINET;
using System;
using System.Collections.Generic;

namespace Camera
{
    public class CameraDaheng : CameraBase
    {
        IGXFactory m_objIGXFactory = null;                            ///<Factory对像
        private IGXDevice objDevice;
        private IGXFeatureControl m_remoteFeatureControl;
        private IGXStream objStream;

        public override event ReadImageDelegate ReadImageEvent;
        public override event ConnectLossDelegate ConnectLossEvent;

        public static string[] DeviceListAcq()
        {
            try
            {
                // initialization
                IGXFactory.GetInstance().Init();

                // Enumerating cameras
                List<IGXDeviceInfo> listGxDeviceInfo = new List<IGXDeviceInfo>();
                IGXFactory.GetInstance().UpdateAllDeviceList(300, listGxDeviceInfo);
                if (listGxDeviceInfo.Count < 1)
                {
                    IGXFactory.GetInstance().Uninit();
                    return null;
                }

                int index = 0;
                string[] deviceList = new string[listGxDeviceInfo.Count];
                foreach (IGXDeviceInfo deviceInfo in listGxDeviceInfo)
                {
                    deviceList[index] = deviceInfo.GetSN();
                    ++index;
                }
                return deviceList;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public override bool Open(string deviceSN)
        {
            try
            {
                // initialization
                IGXFactory.GetInstance().Init();

                // Enumerating cameras
                List<IGXDeviceInfo> listGxDeviceInfo = new List<IGXDeviceInfo>();
                IGXFactory.GetInstance().UpdateAllDeviceList(300, listGxDeviceInfo);

                objDevice = IGXFactory.GetInstance().OpenDeviceBySN(deviceSN, GX_ACCESS_MODE.GX_ACCESS_CONTROL);
                IGXFeatureControl m_remoteFeatureControl = objDevice.GetRemoteFeatureControl();

                // Restore default parameter group
                //m_remoteFeatureControl.GetEnumFeature("UserSetSelector").SetValue("Default");
                //m_remoteFeatureControl.GetCommandFeature("UserSetLoad").Execute();

                // Open the specified flow channel
                objStream = objDevice.OpenStream(0);

                bool bImplemented = objDevice.GetFeatureControl().IsImplemented("EnableAutoConnection");
                if (bImplemented)
                {
                    objDevice.GetFeatureControl().GetBoolFeature("EnableAutoConnection").SetValue(true);
                    DeviceReconnectDelegate reconnectDelegate =
                    new DeviceReconnectDelegate(__OnDeviceReconnectCallbackFun);
                    DeviceDisconnectDelegate disconnectDelegate =
                    new DeviceDisconnectDelegate(__OnDeviceDisconnectCallbackFun);

                    // Register reconnect callback
                    objDevice.RegisterDeviceReconnectCallback(null, reconnectDelegate);
                    // Register disconnect callback
                    objDevice.RegisterDeviceDisconnectCallback(null, disconnectDelegate);
                }

                objStream.RegisterCaptureCallback(this, __CaptureCallbackPro);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        /// <summary>
        /// 重连回调函数
        /// </summary>
        /// <param name="pUserParam">用户私有参数</param>
        private static void __OnDeviceReconnectCallbackFun(object pUserParam)
        {

        }

        /// <summary>
        /// 断线回调函数
        /// </summary>
        /// <param name="pUserParam">用户私有参数</param>
        private static void __OnDeviceDisconnectCallbackFun(object pUserParam)
        {

        }

        /// <summary>
        /// 回调函数,用于获取图像信息和显示图像
        /// </summary>
        /// <param name="obj">用户自定义传入参数</param>
        /// <param name="objIFrameData">图像信息对象</param>
        private void __CaptureCallbackPro(object objUserParam, IFrameData objIFrameData)
        {
            ReadImageEvent(new Imgdata { data = objIFrameData.GetBuffer(), width = (int)objIFrameData.GetWidth(), height = (int)objIFrameData.GetHeight() });
        }

        public override bool Close()
        {
            // Unregister reconnect callback
            objDevice.UnregisterDeviceReconnectCallback();
            // Unregister disconnect callback
            objDevice.UnregisterDeviceDisconnectCallback();
            objStream.Close();
            objDevice.Close();
            return true;
        }

        public override bool StartGrab()
        {
            objStream.StartGrab();
            m_remoteFeatureControl.GetCommandFeature("AcquisitionStart").Execute();
            return true;
        }

        public override void StopGrab()
        {
            m_remoteFeatureControl.GetCommandFeature("AcquisitionStop").Execute();
            objStream.StopGrab();
        }

        public override double GetParameter(string key)
        {
            throw new NotImplementedException();
        }

        public override string GetSerialNumber()
        {
            throw new NotImplementedException();
        }

        public override void LoadConfigurationFromFile(string fileName)
        {
            throw new NotImplementedException();
        }

        public override void SetParameter(string key, double fValue)
        {
            throw new NotImplementedException();
        }

        public override void SetTriggerMode(TriggerMode triggerMode)
        {
            throw new NotImplementedException();
        }

        public override void SetTriggerSource(TriggerSource triggerSource)
        {
            throw new NotImplementedException();
        }

        public override bool TriggerSoftware()
        {
            throw new NotImplementedException();
        }

        public override void UserSave()
        {
            throw new NotImplementedException();
        }
    }
}
